package com.ruoyi.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.SysInfoColumn;
import com.ruoyi.info.service.ISysInfoColumnService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯栏目Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/info/column")
public class SysInfoColumnController extends BaseController
{
    @Autowired
    private ISysInfoColumnService sysInfoColumnService;

    /**
     * 查询资讯栏目列表
     */
    @PreAuthorize("@ss.hasPermi('info:column:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysInfoColumn sysInfoColumn)
    {
        startPage();
        List<SysInfoColumn> list = sysInfoColumnService.selectSysInfoColumnList(sysInfoColumn);
        return getDataTable(list);
    }

    /**
     * 导出资讯栏目列表
     */
    @PreAuthorize("@ss.hasPermi('info:column:export')")
    @Log(title = "资讯栏目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysInfoColumn sysInfoColumn)
    {
        List<SysInfoColumn> list = sysInfoColumnService.selectSysInfoColumnList(sysInfoColumn);
        ExcelUtil<SysInfoColumn> util = new ExcelUtil<SysInfoColumn>(SysInfoColumn.class);
        util.exportExcel(response, list, "资讯栏目数据");
    }

    /**
     * 获取资讯栏目详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
        return success(sysInfoColumnService.selectSysInfoColumnByColumnId(columnId));
    }

    /**
     * 新增资讯栏目
     */
    @PreAuthorize("@ss.hasPermi('info:column:add')")
    @Log(title = "资讯栏目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysInfoColumn sysInfoColumn)
    {
        if ("1".equals(sysInfoColumnService.checkColumnCodeUnique(sysInfoColumn)))
        {
            return error("新增栏目'" + sysInfoColumn.getColumnName() + "'失败，栏目编码已存在");
        }
        sysInfoColumn.setCreateBy(getUsername());
        return toAjax(sysInfoColumnService.insertSysInfoColumn(sysInfoColumn));
    }

    /**
     * 修改资讯栏目
     */
    @PreAuthorize("@ss.hasPermi('info:column:edit')")
    @Log(title = "资讯栏目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysInfoColumn sysInfoColumn)
    {
        if ("1".equals(sysInfoColumnService.checkColumnCodeUnique(sysInfoColumn)))
        {
            return error("修改栏目'" + sysInfoColumn.getColumnName() + "'失败，栏目编码已存在");
        }
        sysInfoColumn.setUpdateBy(getUsername());
        return toAjax(sysInfoColumnService.updateSysInfoColumn(sysInfoColumn));
    }

    /**
     * 删除资讯栏目
     */
    @PreAuthorize("@ss.hasPermi('info:column:remove')")
    @Log(title = "资讯栏目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnIds}")
    public AjaxResult remove(@PathVariable Long[] columnIds)
    {
        return toAjax(sysInfoColumnService.deleteSysInfoColumnByColumnIds(columnIds));
    }

    /**
     * 获取栏目下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysInfoColumn sysInfoColumn)
    {
        List<SysInfoColumn> columns = sysInfoColumnService.selectSysInfoColumnList(sysInfoColumn);
        return success(sysInfoColumnService.buildColumnTreeSelect(columns));
    }
}
